<template>
  <div class="users">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form inline style="margin-top:15px">
      <el-form-item>
        <el-input v-model="query" @keyup.enter.native="searchUsers">
          <el-button slot="append" icon="el-icon-search" @click="searchUsers"></el-button>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="dialogVisible = true">添加用户</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="obj.users" height="calc(100vh - 280px)" border style="width: 100%;">
      <el-table-column type="index" label="#" width="50"></el-table-column>
      <el-table-column prop="username" label="姓名" width="180"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
      <el-table-column prop="mobile" label="电话"></el-table-column>
      <el-table-column label="创建日期">
        <template slot-scope="scope">{{scope.row.create_time|dealDate}}</template>
      </el-table-column>
      <el-table-column label="用户状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.mg_state"
            @click.native="updateUserState(scope.row)"
            active-color="#13ce66"
            inactive-color="#ff4949"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="mobile" label="操作">
        <template slot-scope="obj">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click.native="showUserMsg(obj.row)"
            circle
          ></el-button>
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click.native="handleDelete(obj.row.id)"
            circle
          ></el-button>
          <el-button
            type="success"
            size="small"
            icon="el-icon-check"
            @click.native="handleDistribute(obj.row)"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 15px;"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="params.pagenum"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="params.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="obj.total"
    ></el-pagination>
    <el-dialog title="新增用户" :visible.sync="dialogVisible" width="600px">
      <el-form :model="userForm" ref="userForm" :rules="userFormRules" label-width="120px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="userForm.password"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="userForm.mobile"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="userForm.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="编辑用户" :visible.sync="editDialogVisible" width="600px">
      <el-form :model="editUserForm" ref="editUserForm" label-width="120px">
        <el-form-item label="用户名">
          <el-input v-model="editUserForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="editUserForm.mobile"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="editUserForm.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEdit">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="分配角色" :visible.sync="distributeDialogVisible" width="600px">
      <el-form :model="editUserForm" ref="editUserForm" label-width="120px">
        <el-form-item label="用户名">
          <el-input v-model="distributeUserForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="distributeUserForm.rid" placeholder="请选择">
            <el-option :value="-1" label="请选择"></el-option>
            <el-option
              v-for="item in rolesOptions"
              :key="item.id"
              :label="item.roleName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="distributeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="distributeRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUsers,
  addUsers,
  updateUserState,
  editUser,
  deleteUser,
  getUsersInfo,
  getRolesLists,
  distributeRole
} from "../http/apis";
export default {
  data() {
    return {
      params: {
        query: "",
        pagenum: 1,
        pagesize: 8
      },
      query: "",
      obj: {},
      dialogVisible: false,
      userForm: {
        username: "",
        password: "",
        mobile: "",
        email: ""
      },
      userFormRules: {
        username: [
          {
            required: true,
            pattern: /^[a-z]+$/,
            message: "用户名不合法"
          }
        ],
        password: [
          {
            required: true,
            message: "请输入密码"
          }
        ]
      },
      editDialogVisible: false,
      editUserForm: {
        username: "",
        mobile: "",
        email: "",
        id: ""
      },
      distributeDialogVisible: false,
      distributeUserForm: {
        username: "",
        id: "",
        rid: ""
      },
      rolesOptions: []
    };
  },
  methods: {
    handleSizeChange(val) {
      this.params.pagesize = val;
      this.getUsers();
    },
    handleCurrentChange(val) {
      this.params.pagenum = val;
      this.getUsers();
    },
    //获取用户列表
    async getUsers() {
      const res = await getUsers(this.params);
      console.log(res);
      this.obj = res;
    },
    searchUsers() {
      this.params.query = this.query;
      this.getUsers();
    },
    async addUsers(data) {
      return await addUsers(data);
    },
    handleAdd() {
      this.$refs.userForm.validate(async valid => {
        if (valid) {
          await this.addUsers(this.userForm);
          this.dialogVisible = false;
          this.params = Object.assign(this.params, {
            query: "",
            pagenum: 1,
            pagesize: 2
          });
          this.getUsers();
        } else {
          return false;
        }
      });
    },
    //更新用户状态
    async updateUserState(obj) {
      await updateUserState(obj.id, obj.mg_state);
      this.getUsers();
    },
    //编辑用户信息按钮操作
    showUserMsg(obj) {
      this.editDialogVisible = true;
      const { username, mobile, email, id } = obj;
      this.editUserForm = Object.assign(this.editUserForm, {
        username,
        email,
        mobile,
        id
      });
    },
    //编辑用户确认操作
    async handleEdit() {
      await editUser(this.editUserForm);
      this.getUsers();
      this.editDialogVisible = false;
    },
    // 删除用户操作
    async handleDelete(id) {
      // this.$confirm("是否永久删除该用户?", "提示", {
      //   confirmButtonText: "确定",
      //   cancelButtonText: "取消",
      //   type: "warning"
      // })
      //   .then(() => {
      //     this.deleteUser(id)
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: "info",
      //       message: "已取消删除"
      //     });
      //   });
      try {
        await this.$confirm("是否永久删除该用户?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        });
        this.deleteUser(id);
      } catch (err) {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      }
    },
    async deleteUser(id) {
      await deleteUser(id);
      this.getUsers();
    },
    handleDistribute(obj) {
      this.getRolesLists();
      this.distributeDialogVisible = true;
      const { username, id } = obj;
      this.distributeUserForm = Object.assign(this.distributeUserForm, {
        username,
        id
      });
      this.getUsersInfo(this.distributeUserForm.id);
    },
    async getUsersInfo(id) {
      const { rid } = await getUsersInfo(id);
      // this.distributeUserForm.rid = rid
      this.distributeUserForm = Object.assign(this.distributeUserForm, {
        rid
      });
    },
    //获取角色列表
    async getRolesLists() {
      const data = await getRolesLists();
      this.rolesOptions = data;
    },
    //提交用户角色修改
    async distributeRole() {
      const { id, rid } = this.distributeUserForm;
      await distributeRole(id, rid);
      this.distributeDialogVisible = false
      this.getUsers();
    }
  },
  created() {
    this.getUsers();
  }
};
</script>
<style>
.users {
  background-color: #fff;
  border-radius: 5px;
  padding: 15px;
}
</style>